<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
      label-width="90px">
      <el-row>

          <el-form-item label="链接内容" prop="urlValue">
            <el-input v-model="queryParams.urlValue" placeholder="请输入链接内容" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>

          <el-form-item label="状态" prop="dataStatus">
            <el-select v-model="queryParams.dataStatus" placeholder="请选择状态" clearable size="small">
              <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>

      </el-row>
    </el-form>
    <el-row :gutter="10" class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['platform:templateurl:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="divTable">
      <el-table v-loading="loading" :data="templateurlList" row-key="urlUid" default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column label="链接内容" align="center" prop="urlValue" />
        <el-table-column label="状态" align="center" prop="dataStatus">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.dataStatus" active-value="1" inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="dataPriority" />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}  {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['platform:templateurl:edit']">修改</el-button>
            <el-button v-if="scope.row.urlType == 'type'" size="mini" type="text" icon="el-icon-plus"
              @click="handleAddd(scope.row)" v-hasPermi="['platform:templateurl:add']">新增</el-button>
            <el-button v-if="scope.row.dataStatus == 0" size="mini" type="text" icon="el-icon-delete"
              @click="handleDelete(scope.row)" v-hasPermi="['platform:templateurl:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加或修改平台_模板链接配置对话框 -->


    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级链接" prop="urlPuid">
          <el-select v-model="form.urlPuid" placeholder="请选择" clearable size="small">
            <el-option v-for="dict in urlType" :key="dict.urlUid" :label="dict.urlValue" :value="dict.urlUid" />
          </el-select>
        </el-form-item>

        <el-form-item label="跳转类型" prop="jumpType">
          <el-select v-model="form.jumpType" placeholder="请选择跳转类型" clearable size="small" class="input-rule">
            <el-option v-for="dict in jumpTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="跳转标识" prop="jumpSign">
          <el-select v-model="form.jumpSign" placeholder="请选择跳转标识" clearable size="small" class="input-rule">
            <el-option v-for="dict in jumpSignOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="链接内容" prop="urlValue">
          <el-input v-model="form.urlValue" placeholder="请输入链接内容" class="select-normal" />
        </el-form-item>

        <el-form-item label="链接地址" prop="urlAddress">
          <el-input v-model="form.urlAddress" placeholder="链接地址" class="select-normal" />
        </el-form-item>

        <el-form-item label="排序" prop="dataPriority">
          <el-input-number v-model="form.dataPriority" size="small" placeholder="请输入排序" controls-position="right"
            :min="0" class="select-normal" :max="999999" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listTemplateurl,
    getTemplateurl,
    delTemplateurl,
    addTemplateurl,
    updateTemplateurl,
    exportTemplateurl,
    updateUrlStatus
  } from "@/api/platform/templateurl";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Templateurl",
    components: {
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 平台_模板链接配置表格数据
        templateurlList: [],
        // 平台_模板链接配置树选项
        templateurlOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态(1有效/0无效/删除-1)字典
        dataStatusOptions: [],
        // 查询参数
        queryParams: {
          urlPuid: null,
          platFormUid: null,
          urlType: null,
          urlValue: null,
          dataStatus: null,
          dataPriority: null,
          createUid: null,
          createName: null,
          createTime: null,
          updateUid: null,
          updateName: null,
          updateTime: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          urlValue: [{
            required: true,
            message: "链接内容不能为空",
            trigger: "blur"
          }],
          dataPriority: [{
            required: true,
            message: "排序不能为空",
            trigger: "blur"
          }],
          urlPuid: [{
            required: true,
            message: "上级链接不能为空",
            trigger: "blur"
          }],

        },
        urlType: [],
        jumpTypeOptions: [],
        jumpSignOptions: [],
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_data_status").then(response => {
        this.dataStatusOptions = response.data;
      });
      this.getDicts("platform_template_jump_type").then(response => {
        this.jumpTypeOptions = response.data;
      });
      this.getDicts("platform_template_sign").then(response => {
        this.jumpSignOptions = response.data;
      });
    },
    methods: {
      /** 查询平台_模板链接配置列表 */
      getList() {
        this.loading = true;
        listTemplateurl(this.queryParams).then(response => {
          this.templateurlList = this.handleTree(response.data, "urlUid", "urlPuid");
          this.loading = false;
        });
      },
      getSelect() {
        this.queryParams.urlType = 'type';
        listTemplateurl(this.queryParams).then(response => {
          this.urlType = response.data;
          this.open = true;
          this.queryParams.urlType = null;
        });
      },
      /** 转换平台_模板链接配置数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.urlUid,
          label: node.urlValue,
          children: node.children
        };
      },
      /** 查询平台_模板链接配置下拉树结构 */
      getTreeselect() {
        listTemplateurl().then(response => {
          this.templateurlOptions = [];
          const data = {
            urlUid: 0,
            urlValue: '顶级节点',
            children: []
          };
          data.children = this.handleTree(response.data, "urlUid", "urlPuid");
          this.templateurlOptions.push(data);
        });
      },
      // 状态(1有效/0无效/删除-1)字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          urlUid: null,
          urlPuid: null,
          platFormUid: null,
          urlType: null,
          urlValue: null,
          dataStatus: null,
          dataPriority: null,
          createUid: null,
          createName: null,
          createTime: null,
          updateUid: null,
          updateName: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();

        this.urlType = [{
          urlValue: '顶级链接',
          urlUid: 0,
        }];
        this.form.urlPuid = 0
        this.open = true;
        this.title = "添加模板链接配置";
      },
      handleAddd(row) {
        this.reset();
        this.getTreeselect();
        this.getSelect();
        this.form.urlPuid = row.urlUid
        this.title = "添加模板链接配置";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        if (row.urlPuid == 0 || row.urlPuid == "") {
          this.urlType = [{
            urlValue: '顶级链接',
            urlUid: 0,
          }];
        } else {
          this.getSelect();
        }
        getTemplateurl(row.urlUid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改模板链接配置";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.urlUid != null) {
              updateTemplateurl(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              if (!this.form.urlPuid) {
                this.form.urlPuid = 0;
              }
              if (this.form.urlPuid == 0) {
                this.form.urlType = 'type';
              } else {
                this.form.urlType = 'page';
              }
              addTemplateurl(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除链接内容为"' + row.urlValue + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTemplateurl(row.urlUid);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      }, // 状态修改
      handleStatusChange(row) {
        let text = row.dataStatus === "0" ? "无效" : "有效";
        this.$confirm('确定要把链接内容为"' + row.urlValue + '",设为' + text + '吗？', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateUrlStatus(row.urlUid, row.dataStatus);
        }).then(() => {
          this.msgSuccess("操作成功");
          this.getList();
        }).catch(function() {
          row.dataStatus = row.dataStatus === "0" ? "1" : "0";
        });
      }
    }
  };
</script>
